package com.serv.util;

import com.jfinal.aop.Aop;
import com.jfinal.kit.StrKit;
import com.serv.common.model.sys.Organization;
import com.serv.common.model.sys.User;
import com.serv.common.service.sys.OrganizationService;
import com.serv.common.service.sys.UserService;

/**
 * 消费者，特指一个人或者一个组织
 * @author ASUS
 *
 */
public class Consumer {
	private String id;
	private String type;
	private String name;
	
	public Consumer(String typeAndId) {
		if(StrKit.isBlank(typeAndId)) throw new RuntimeException("Consumer must have typeAndId");
		typeAndId = typeAndId.replace("：",":");
		
		String preOrg = "org:";
		String preUser = "user:";
		if(typeAndId.startsWith(preOrg)) {
			String orgId = typeAndId.substring(preOrg.length());
			Organization org = Aop.get(OrganizationService.class).findById(orgId);
			setId(org.getStr("id"));
			setType("org");
			setName("组织：" + org.getStr("name"));
		}
		
		if(typeAndId.startsWith(preUser)) {
			String userId = typeAndId.substring(preUser.length());
			User user = Aop.get(UserService.class).findById(userId);
			setId(user.getStr("id"));
			setType("user");
			setName("个人：" + user.getStr("realname"));
		}
	}
	
	public static Consumer NEW(String typeAndId){
		if(StrKit.isBlank(typeAndId)) return null;
		
		return new Consumer(typeAndId);
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
